package com.lins.framework.utils.address;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;


import com.lins.framework.common.Constants;
import com.lins.framework.utils.StoreUtils;
import com.lins.framework.utils.Validator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 解析地区三级联动类
 * Created by LinS on 2016/9/12.
 */

public class AreaParsingPresenter {
    private Context context;

    private ArrayList<AreaParsingBean> provinceList;
    private ArrayList<AreaParsingBean> cityList;
    private ArrayList<AreaParsingBean> countyList;

    // 存储省对应的所有市
    private Map<String, ArrayList<AreaParsingBean>> mCitiesDataMap = new HashMap<>();
    // 存储市对应的所有区
    private Map<String, ArrayList<AreaParsingBean>> mCountyDataMap = new HashMap<>();

    public AreaParsingPresenter(Context context) {
        this.context = context;
        JsonCitisData(InitData());
    }

    public ArrayList<AreaParsingBean> getProvinceList() {
        return provinceList;
    }

    public ArrayList<AreaParsingBean> getCityList(String str) {
        return mCitiesDataMap.get(str);
    }

    public ArrayList<AreaParsingBean> getCountyList(String str, String path) {
        if (Validator.isEmpty(mCountyDataMap.get(str))) {
            /**判断第三级列表是否有数据
             *添加一条字符串为空的默认数据
             */
            ArrayList<AreaParsingBean> newList = new ArrayList<>();
            AreaParsingBean newBean = new AreaParsingBean("", path + ",0");
            newList.add(newBean);
            return newList;
        }
        return mCountyDataMap.get(str);
    }

    /**
     * 解析三级联动的城市信息
     *
     * @param cityJson
     */
    private void JsonCitisData(String cityJson) {
        if (!TextUtils.isEmpty(cityJson)) {
            try {
                JSONObject object = new JSONObject(cityJson);
                JSONArray array = object.getJSONArray("data");

                provinceList = new ArrayList<>();
                // 循环遍历
                for (int i = 0; i < array.length(); i++) {
                    AreaParsingBean provincePOJO = new AreaParsingBean();
                    JSONObject mProvinceObject = array.getJSONObject(i);
                    provincePOJO.setTitle(mProvinceObject.optString("title"));
                    provincePOJO.setPath(mProvinceObject.optString("path"));
                    provinceList.add(provincePOJO);
                    String mProvinceStr = mProvinceObject.optString("title");
                    JSONArray cityArray;
                    try {
                        // 循环遍历省对应下的城市
                        cityArray = mProvinceObject.getJSONArray("city");
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                    cityList = new ArrayList<>();
                    for (int j = 0; j < cityArray.length(); j++) {
                        AreaParsingBean cityPOJO = new AreaParsingBean();
                        // 循环遍历城市，并将城市保存在mCitiesDatas[]中
                        JSONObject mCityObject = cityArray.getJSONObject(j);
                        cityPOJO.setTitle(mCityObject.optString("title"));
                        cityPOJO.setPath(mCityObject.optString("path"));
                        cityList.add(cityPOJO);
                        String mCityStr = mCityObject.optString("title");
                        // 循环遍历地区
                        JSONArray areaArray;
                        try {
                            // 判断是否含有第三级区域划分，若没有，则跳出本次循环，重新执行循环遍历城市
                            areaArray = mCityObject.getJSONArray("county");
                        } catch (Exception e) {
                            e.printStackTrace();
                            continue;
                        }
                        countyList = new ArrayList<>();
                        // 执行下面过程则说明存在第三级区域
                        for (int m = 0; m < areaArray.length(); m++) {
                            AreaParsingBean countyPOJO = new AreaParsingBean();
                            JSONObject areaObject = areaArray.getJSONObject(m);
                            countyPOJO.setTitle(areaObject.optString("title"));
                            countyPOJO.setPath(areaObject.optString("path"));
                            countyList.add(countyPOJO);
                        }

                        // 存储市对应的所有第三级区域
                        mCountyDataMap.put(mCityStr, countyList);
                    }
                    // 存储省份对应的所有市
                    mCitiesDataMap.put(mProvinceStr, cityList);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 读取本地省市县文件
     */
    private String InitData() {
        String fileName = "areainfo.txt";//存储的文件名
        String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + Constants.AREA_INFO_PATH;
        String path = StoreUtils.getSaveFilePath(filePath, fileName);
        String areainfo = (String) StoreUtils.restoreObject(path);
        return areainfo;
    }


}
